package com.shiminghui;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;


public class Test7 {
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://speedtest.net/speedtest-config.php");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");

        long startTime = System.currentTimeMillis();
        int responseCode = con.getResponseCode();
        long endTime = System.currentTimeMillis();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            long bytesRead = con.getContentLength();
            long timeTaken = endTime - startTime;
            double speed = (bytesRead / 1024.0 / 1024.0) / (timeTaken / 1000.0);
            System.out.println("Downloaded " + bytesRead + " bytes in " + timeTaken + "ms (" + speed + " MB/s)");
        } else {
            System.out.println("Failed to download file: HTTP error code : " + responseCode);
        }
    }
}
